<?php
class DepotManagement extends SBKH{	
	/* District */
	public function index($page_number = ""){
		if(!isset($_SESSION['user'])) $this->redirect('login');
		$this->SetLayout('default');
		
		$province_id = "";
		$province_name = "";
		$district_id = "";
		$district_name = "";
		$str = "";
		if(!empty($_POST)){//die(pr($_POST));
			$province_id = isset($_POST['cbo-province'])? getURL('cbo-province'): @$_SESSION['criteria']['province_id'];
			$province_name = isset($_POST['cbo-province-name'])? getURL('cbo-province-name'): @$_SESSION['criteria']['province_name'];
			$district_id = isset($_POST['cbo-district'])? getURL('cbo-district'): @$_SESSION['criteria']['district_id'];
			$district_name = isset($_POST['cbo-district-name'])? getURL('cbo-district-name'): @$_SESSION['criteria']['district_name'];
			$str = isset($_POST['txt-search'])? getURL('txt-search'): @$_SESSION['criteria']['str'];
			if(isset($_SESSION['criteria'])){unset($_SESSION['criteria']);}		
			if($str == "" && $province_id == "" && $province_name == "" && $district_id == "" && $district_name == ""){
				//die("str = " . $str . " proid = " . $province_id . " pro = " . $province_name . " dist = " . $district_id . " dis = " . $district_name);
				unset($_SESSION['criteria']);
				$this -> Redirect('');
			}else{
				//die("str = " . $str . " proid = " . $province_id . " pro = " . $province_name . " dist = " . $district_id . " dis = " . $district_name);
			}
		}
		$obj = $this -> LoadModel('DepotManagement');
		
		$limit_start = 0;// for pagination
		$limit_end = 15;// for pagination
		$page_number = $page_number != ""?$page_number:1;// for pagination
		if($page_number != 1)// for pagination
			$limit_start = $limit_end * $page_number - $limit_end;	// for pagination
		
		$results = $obj->getDistrictRecords($province_id, $district_id, $str, $limit_start, $limit_end);
		$records = $results[0];
		$number_of_rows = $results[1];// for pagination
			
		$this->SetVar('page_number',$page_number);// for pagination
		$this->SetVar('limit_start',$limit_start);// for pagination
		$this->SetVar('limit_end',$limit_end);// for pagination
		$this->SetVar('number_of_rows',$number_of_rows);// for pagination
		
		$provinces = $this -> Query("SELECT `id`, `province_name` FROM `tbl_province` ORDER BY  `province_name` ASC");
		if($province_id == ""){
			$districts = $this -> Query("SELECT `id`, `district_name` FROM `tbl_district` ORDER BY  `district_name` ASC");
		}else{
			$districts = $this -> Query("SELECT `id`, `district_name` FROM `tbl_district` WHERE `province_id` = '$province_id' ORDER BY  `district_name` ASC");
		}
		//$records = $obj -> getDistrictRecords();
		$this -> SetVar('provinces', $provinces);
		$this -> SetVar('districts', $districts);
		$this -> SetVar('records',$records);
		$this -> SetVar('str',$str);
		$this -> SetVar('province_id', $province_id);
		$this -> SetVar('province_name', $province_name);
		$this -> SetVar('district_id', $district_id);
		$this -> SetVar('district_name', $district_name);
		$this -> SetVar('page_title', "Districts Information");
		$this -> SetVar('menu_focus', 'list-depot');
		
		$_SESSION['criteria']['str'] = $str;
		$_SESSION['criteria']['province_id'] = $province_id;
		$_SESSION['criteria']['province_name'] = $province_name;
		$_SESSION['criteria']['district_id'] = $district_id;
		$_SESSION['criteria']['district_name'] = $district_name;
		$_SESSION['criteria']['page_number'] = $page_number;
		
		$this -> View('index','left-menu');
	}
	/*
	 * Add new district info
	*/
	public function add(){
		if(empty($_POST)){
			$this -> SetLayout('default');
			$this -> SetVar('page_title', "Add New Depot");
			$obj = $this -> LoadModel('DepotManagement');
			$provinces = $this -> Query("SELECT `id`, `province_name` FROM `tbl_province` ORDER BY  `province_name` ASC");
			$districts = $this -> Query("SELECT `id`, `district_name` FROM `tbl_district` ORDER BY  `district_name` ASC");
			$this -> SetVar('provinces', $provinces);
			$this -> SetVar('districts', $districts);
			$this -> SetVar('menu_focus', 'list-depot');
			$this -> view('add', 'left-menu');
		}else{
			$_POST['data']['depot']['created_by'] = $_SESSION['user']['id'];
			$_POST['data']['depot']['modified_by'] = $_SESSION['user']['id'];
			$this -> Save(($this -> data()));
			$this -> Redirect('');
		}
	}
	/*
	 * Edit District info
	* Param: district id
	*/
	public function edit($depot_id = 0){
		if(empty($_POST)){
			if($depot_id != 0){
				$this -> SetLayout('default');
				$this -> SetVar('page_title', "Update Depot Info");
				$obj = $this -> LoadModel('DepotManagement');
				$provinces = $this -> Query("SELECT `id`, `province_name` FROM `tbl_province` ORDER BY  `province_name` ASC");
				$districts = $this -> Query("SELECT `id`, `district_name` FROM `tbl_district` ORDER BY  `district_name` ASC");
							
				$records = $obj -> getRecordByID($depot_id);
				$this -> SetVar('record', $records[0]);
				$this -> SetVar('provinces', $provinces);
				$this -> SetVar('districts', $districts);
				$this -> SetVar('menu_focus', 'list-depot');
				$this -> view('edit', 'left-menu');
			}else{
				$this -> Redirect('');
			}
		}else{
			$_POST['data']['depot']['modified_by'] = $_SESSION['user']['id'];
			$this -> Update(($this -> data()),false);
			$this -> Redirect('');
		}
	}
	/*
	 * Delete province
	* Param: province id
	*/
	public function delete($pro_id = 0){
		$obj = $this -> LoadModel('DepotManagement');
		$obj->delete($pro_id);
		$this->redirect('');
	}	
	/*
	 * Get Districts by Province ID
	 * Parma: province id
	 */
	public function get_districts(){
		$pro_id = GetURL('sb_proId');
		$this -> SetLayout(false);
		$obj = $this -> LoadModel('DepotManagement');
		$records = $obj -> getDistrictByProvinceID($pro_id);
		$option = "";
		$option .= '<option value="">Select Distict</option>';
		if(count($records)){			
			foreach($records as $record){
				$option .= '<option value="' . $record -> id . '">' . $record -> district_name . '</option>'; 
			}
		}
		echo $option;
	}
}
?>